﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FarseerPhysics.SamplesFramework;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;

namespace game.Blocks
{
    public class CGroundBlock :  CBaseBlock
    {
        /*PolygonShape shape;
        Fixture fixture;*/
        public CGroundBlock(World world, Vector2 position) : base(world, position,true)
        {
            oOrginalPos = _sprite.Body.Position;
            oRandom = new Random(DateTime.Now.Millisecond);
            SetNewDirection();
            _sprite.Body.BodyType = BodyType.Static;
            //_sprite.Body.IgnoreGravity = true;
            
            /*Vertices vertices = new Vertices();
            vertices.Add(new Vector2(ConvertUnits.ToSimUnits(position.X - _sprite.Texture.Width), ConvertUnits.ToSimUnits((position.Y - _sprite.Texture.Height))));
            vertices.Add(new Vector2(ConvertUnits.ToSimUnits(position.X + _sprite.Texture.Width), ConvertUnits.ToSimUnits((position.Y - _sprite.Texture.Height))));
            vertices.Add(new Vector2(ConvertUnits.ToSimUnits(position.X + _sprite.Texture.Width), ConvertUnits.ToSimUnits((position.Y + _sprite.Texture.Height))));
            vertices.Add(new Vector2(ConvertUnits.ToSimUnits(position.X - _sprite.Texture.Width), ConvertUnits.ToSimUnits((position.Y + _sprite.Texture.Height))));
            
            shape = new PolygonShape(vertices, 1);
            fixture = _sprite.Body.CreateFixture(shape);*/

            _sprite.Body.OnCollision += Body_OnCollision;
            world.ContactManager.OnBroadphaseCollision += MyOnBroadphaseCollision;
        }

        private bool Body_OnCollision(Fixture fixturea, Fixture fixtureb, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            Console.WriteLine("collission A");
            oDirection = -oDirection;
            back = true;

            return true;
        }

        public void MyOnBroadphaseCollision(ref FixtureProxy fp1, ref FixtureProxy fp2)
        {
            //Console.WriteLine("collission B");
            oDirection = -oDirection;
            back = true;
        }

        public void SetNewDirection()
        {
            do
            {
                switch (oRandom.Next(4))
                {
                    case 0:
                        oDirection = new Vector2(-1f, 0f);
                        break;
                    case 1:
                        oDirection = new Vector2(0f, -1f);
                        break;
                    case 2:
                        oDirection = new Vector2(1f, 0f);
                        break;
                    case 3:
                        oDirection = new Vector2(0f, 1f);
                        break;
                }
            }
            while (oDirection == oLastDirection);
            oLastDirection = oDirection;
        }

        public override void Update(float fDeltaT)
        {
            float fDistance = (_sprite.Body.Position - oOrginalPos).Length();
            if (fDistance > ofMaxDistance)
            {
                oDirection = -oDirection;
                back = true;
            }
            if (back == true && fDistance < 0.01f)
            {
                _sprite.Body.Position = oOrginalPos;
                SetNewDirection();
                back = false;
            }
            _sprite.Body.Position += oDirection * fDeltaT * ofSpeedFactor;
            
            /*Vector2 move =  oDirection * fDeltaT * ofSpeedFactor;
            shape.Vertices.ForEach(delegate(Vector2 v)
            {
                v += move;
            });*/
        }

        private Vector2 oDirection;
        private Vector2 oLastDirection;
        private const float ofSpeedFactor = 1f;
        private const float ofMaxDistance = 1f;
        private Vector2 oOrginalPos;
        private Random oRandom;
        private bool back;
    }
}
